home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / GR / GR_TAM.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  27KB  |  1,133 lines

  1. /****************************************************************
  2.  
  3.     gr_tam.c    implementation of Bywater graphics
  4.             standard for AT&T Unix PC using
  5.             tam() and wrastop() libraries
  6.  
  7.             Copyright (c) 1991, Ted A. Campbell
  8.  
  9.             Bywater Software
  10.             P. O. Box 4023 
  11.             Duke Station 
  12.             Durham, NC  27706
  13.  
  14.             email: tcamp@hercules.acpub.duke.edu
  15.  
  16.     Copyright and Permissions Information:
  17.  
  18.     All U.S. and international copyrights are claimed by the
  19.     author. The author grants permission to use this code
  20.     and software based on it under the following conditions:
  21.     (a) in general, the code and software based upon it may be 
  22.     used by individuals and by non-profit organizations; (b) it
  23.     may also be utilized by governmental agencies in any country,
  24.     with the exception of military agencies; (c) the code and/or
  25.     software based upon it may not be sold for a profit without
  26.     an explicit and specific permission from the author, except
  27.     that a minimal fee may be charged for media on which it is
  28.     copied, and for copying and handling; (d) the code must be 
  29.     distributed in the form in which it has been released by the
  30.     author; and (e) the code and software based upon it may not 
  31.     be used for illegal activities. 
  32.  
  33.     This file specifies the interface for graphics and mouse 
  34.     handling utilized in the Space Flight Simulator.  This file
  35.     may be utilized as the basis for implementations of gr and 
  36.     thus of sfs on varied computers.  If you can implement the
  37.     functions specified in this file, sfs should be implemented.
  38.  
  39. ****************************************************************/
  40.  
  41. #include "stdio.h"
  42. #include "fcntl.h"
  43. #include "tam.h"
  44. #include "kcodes.h"
  45. #include "message.h"
  46. #include "errno.h"
  47. #include "sys/window.h"
  48. #include "sys/font.h"
  49. #include "gr.h"
  50. #include "bw.h"
  51.  
  52. static struct fntdef *tam_fsmall; /* ptr to small fntdef struct */
  53. static struct fntdef *tam_fmed;      /* ptr to medium fntdef struct */
  54. static struct fntdef *tam_flarge; /* ptr to large fntdef struct */
  55. static struct fntdef *tam_fhuge;  /* ptr to huge fntdef struct */
  56. static struct fntdef *tam_fdef;   /* ptr to selected fntdef struct */
  57.  
  58. #define IMAGES          64
  59. #define    FULLSCREEN    FALSE
  60. #define    CHECK_PARAMS    TRUE
  61. #define SMALLFONT       "/usr/lib/wfont/smallfont"
  62. #define MEDFONT         "/usr/lib/wfont/system.8.ft"
  63. #define LARGEFONT       "/usr/lib/wfont/PLAIN.R.E.18.A"
  64. #define HUGEFONT        "/usr/lib/wfont/PLAIN.R.E.24.A"
  65. #define    ADD_SIZE    1
  66. #define ADD_HIDDEN    2
  67. #define    WRITE_OUTPUT    FALSE
  68.  
  69. int    gr_colors = 2;
  70. int    gr_pxsize = 287;
  71. int    gr_pysize = 428;
  72. int    gr_ismouse = TRUE;
  73. int     gr_clipping = FALSE;
  74. int     gr_blitting = TRUE;
  75. int     gr_saving = TRUE;
  76. int    gr_screens = 2;
  77.  
  78. int w, w_id;
  79. static unsigned short tam_pixel[ 2 ] = { 0xffff, 0xffff };
  80. static struct gr_window *grwind;
  81. static unsigned short *tam_selected;
  82. static unsigned short *tam_hidden;
  83. int tam_mready = FALSE;        /* is mouse input ready */
  84. int tam_mx, tam_my;        /* mouse position x, y */
  85. char    *tam_images[ IMAGES ];  /* array of pointers to image buffers */
  86.  
  87. extern unsigned short patblack[];
  88. extern unsigned short patwhite[];
  89. extern unsigned short patgray[];
  90. extern unsigned short patltgray[];
  91.  
  92. extern char * malloc();
  93. extern unsigned short tam_words();
  94. extern long tam_imsize();
  95.  
  96. #ifdef    WRITE_OUTPUT
  97. extern FILE *outfile;
  98. #endif
  99.  
  100. /****************************************************************
  101.  
  102.     gr_init()
  103.  
  104. ****************************************************************/
  105.  
  106. gr_init( grwindow, font_path )
  107.    struct gr_window *grwindow;
  108.    char * font_path;
  109.    {
  110.    static struct uwdata uw;
  111.    static struct umdata um;
  112.    register int i;
  113.    int r;
  114.  
  115.    grwind = grwindow;
  116.  
  117.    close( 0 );
  118.    close( 1 );
  119.    close( 2 );
  120.    w_id = open( "/dev/window", O_RDWR );
  121.    dup( w_id );
  122.    dup( w_id );
  123.  
  124.    winit();
  125.    
  126. #if    FULLSCREEN
  127.    w = wcreate( 1, 0, 24, 80, NBORDER );
  128. #else
  129.    w = wcreate( 2, 2, 19, 70, BORDCANCEL );
  130. #endif
  131.    wuser( w, "Bywater GR Implementation" );
  132.    wlabel( w, "Bywater GR Implementation" );
  133.    wprintf( w, "\033[=1C" );
  134.    clear();
  135.  
  136.    ioctl( w, WIOCGETD, &uw );
  137.  
  138.    grwindow->xmax = uw.uw_width - 1;
  139.    grwindow->ymax = uw.uw_height - 1;
  140.    grwindow->initialized = TRUE;
  141.    grwindow->font = F_DEFAULT;
  142.  
  143.    tam_readfont( SMALLFONT, &tam_fsmall );
  144.    tam_readfont( MEDFONT, &tam_fmed );
  145.    tam_readfont( LARGEFONT, &tam_flarge );
  146.    tam_readfont( HUGEFONT, &tam_fhuge );
  147.  
  148.    if ( tam_fmed == NULL )
  149.      {
  150.      fprintf( stderr, "ERROR: failed to load system font.\n" );
  151.      wgetc( w );
  152.      wexit( 1 );
  153.      }
  154.  
  155.    tam_fdef = tam_fmed;
  156.    grwindow->fysize = tam_fdef->ff_vs;
  157.    grwindow->fxsize = tam_fdef->ff_hs;
  158.  
  159.    if ( ( tam_hidden = (unsigned short *) 
  160.       malloc( tam_imsize( 0, 0,
  161.       grwind->xmax + ADD_HIDDEN, grwind->ymax + ADD_HIDDEN ) )) == NULL )
  162.       {
  163.       fprintf( stderr, "Failed to find memory for gr_hidden\n" );
  164.       return -1;
  165.       }
  166.  
  167. #ifdef    OLD_DEBUG
  168.       wgoto( w, 0, 0 );
  169.       fprintf( stderr, "test wrastop(): tam_hidden %08lx\n",
  170.          (long) tam_hidden );
  171.       kb_rx();
  172. #endif
  173.  
  174. #ifdef    OLD_DEBUG
  175.    gr_text( GR_PRIMARY, 0, 0, " !\"#", WHITE, BLACK );
  176. #endif
  177.  
  178.    /* set image buffer pointers to NULL */
  179.  
  180.    for ( i = 0; i < IMAGES; ++i )
  181.       {
  182.       tam_images[ i ] = NULL;
  183.       }
  184.  
  185.    um.um_flags = MSDOWN | MSUP;
  186.    um.um_icon = NULL;
  187.    r = wsetmouse( w, &um );
  188.  
  189.    if ( r == -1 )
  190.       {
  191.       fprintf( stderr, "Setmouse() returned -1\n" );
  192.       kb_rx();
  193.       }
  194.  
  195.    return TRUE;
  196.    }
  197.  
  198. /****************************************************************
  199.  
  200.    gr_deinit()
  201.  
  202. ****************************************************************/
  203.  
  204. gr_deinit( screen )
  205.    int screen;
  206.    {
  207.    wdelete( w );
  208.    wexit( 0 );
  209.    }
  210.  
  211. /****************************************************************
  212.  
  213.    gr_cls()
  214.  
  215. ****************************************************************/
  216.  
  217. gr_cls( screen )
  218.    int screen;
  219.    {
  220.  
  221.    if ( screen == GR_PRIMARY )
  222.       {
  223.       clear();
  224.       }
  225.    else
  226.       {
  227.       gr_rectangle( screen, 0, 0, grwind->xmax, grwind->ymax,
  228.          BLACK, SOLID );
  229.       }
  230.    }
  231.  
  232.  
  233. /****************************************************************
  234.  
  235.    gr_pixel()
  236.  
  237. ****************************************************************/
  238.  
  239. gr_pixel( screen, x, y, color )
  240.    int screen;
  241.    int x, y;
  242.    int color;
  243.    {
  244.    int r;
  245.  
  246. #if    CHECK_PARAMS
  247.    if ( ( x < 0 ) || ( x > grwind->xmax ))
  248.       {
  249.       sprintf( bw_ebuf, "[pr:] gr_pixel(): x value is %d", x );
  250.       bw_error( bw_ebuf );
  251.       return BW_ERROR;
  252.       }
  253.    if ( ( y < 0 ) || ( y > grwind->ymax ))
  254.       {
  255.       sprintf( bw_ebuf, "[pr:] gr_pixel(): y value is %d", y );
  256.       bw_error( bw_ebuf );
  257.       return BW_ERROR;
  258.       }
  259. #endif
  260.  
  261.    tam_setscreen( screen );
  262.  
  263.    r = wrastop( w, tam_pixel, (unsigned short) 2,
  264.       tam_selected, tam_words( grwind->xmax + ADD_HIDDEN ),
  265.       (unsigned short) 0, (unsigned short) 0,
  266.       (unsigned short) x, 
  267.       (unsigned short) ( grwind->ymax - y ),
  268.       (unsigned short) ADD_SIZE, (unsigned short) ADD_SIZE,
  269.       SRCSRC, DSTOR, (unsigned short *) 0 );
  270.  
  271. #ifdef    DEBUG
  272.    if ( r == -1 )
  273.       {
  274.       fprintf( stderr, "gr_pixel(): wrastop() failed, error %d.\n", errno );
  275.       kb_rx();
  276.       }
  277. #endif
  278.    }
  279.          
  280. /****************************************************************
  281.  
  282.    gr_line()
  283.  
  284. ****************************************************************/
  285.  
  286. gr_line( screen, x1, y1, x2, y2, color, style )
  287.    int screen;
  288.    int x1, y1, x2, y2;
  289.    int color, style;
  290.    {
  291.  
  292. #if    CHECKPARAMS
  293.    if ( ( x1 < 0 ) || ( x1 > grwind->xmax ))
  294.       {
  295.       sprintf( bw_ebuf, "[pr:] gr_line(): x1 value is %d", x1 );
  296.       bw_error( bw_ebuf );
  297.       return BW_ERROR;
  298.       }
  299.    if ( ( x2 < 0 ) || ( x2 > grwind->xmax ))
  300.       {
  301.       sprintf( bw_ebuf, "[pr:] gr_line(): x2 value is %d", x2 );
  302.       bw_error( bw_ebuf );
  303.       return BW_ERROR;
  304.       }
  305.    if ( ( y1 < 0 ) || ( y1 > grwind->ymax ))
  306.       {
  307.       sprintf( bw_ebuf, "[pr:] gr_line(): y1 value is %d", y1 );
  308.       bw_error( bw_ebuf );
  309.       return BW_ERROR;
  310.       }
  311.    if ( ( y2 < 0 ) || ( y2 > grwind->ymax ))
  312.       {
  313.       sprintf( bw_ebuf, "[pr:] gr_line(): y2 value is %d", y2 );
  314.       bw_error( bw_ebuf );
  315.       return BW_ERROR;
  316.       }
  317. #endif
  318.  
  319.    if ( x1 == x2 )
  320.       {
  321.       gr_rectangle( screen, x1, y1, x2, y2, color, style );
  322.       }
  323.    else if ( y1 == y2 )
  324.       {
  325.       gr_rectangle( screen, x1, y1, x2, y2, color, style );
  326.       }
  327.    else
  328.       {
  329.       def_line( screen, x1, y1, x2, y2, color, style );
  330.       }
  331.    }
  332.  
  333. /****************************************************************
  334.  
  335.    gr_text()
  336.  
  337. ****************************************************************/
  338.  
  339. gr_text( screen, x, y, string, foreground, background )
  340.    int screen;
  341.    int x, y;
  342.    int foreground, background;
  343.    char *string;
  344.    {
  345.    char *s;
  346.    unsigned short c;
  347.    char *m;
  348.    register unsigned int x_pos;
  349. #ifdef    DEBUG
  350.    static int do_it = 0;
  351. #endif
  352.  
  353. #if    CHECK_PARAMS
  354.    if ( ( x < 0 ) || ( x > grwind->xmax ))
  355.       {
  356.       fprintf( stderr, "ERROR: [pr:] gr_text(): x value is %d", x );
  357.       kb_rx();
  358.       return BW_ERROR;
  359.       }
  360.    if ( ( y < 0 ) || ( y > grwind->ymax ))
  361.       {
  362.       fprintf( stderr, "ERROR: [pr:] gr_text(): y value is %d", y );
  363.       kb_rx();
  364.       return BW_ERROR;
  365.       }
  366. #endif
  367.  
  368.    tam_setscreen( screen );
  369.  
  370.    s = string;
  371.    x_pos = x;
  372.    while ( *s != 0 )
  373.       {
  374.       c = *s - 32;
  375.       m = (char *) &(tam_fdef->ff_fc[ c ].fc_mr)
  376.          + ( tam_fdef->ff_fc[ c ].fc_mr );
  377.  
  378.       if ( ( *s > 31 ) && ( *s < 129 ))
  379.          {
  380.          if ( ( x_pos + tam_fdef->ff_hs ) >= grwind->xmax )
  381.             {
  382.             return BW_ERROR;
  383.             }
  384.          tam_char( screen, x_pos, y, m,
  385.             tam_fdef->ff_fc[ c ].fc_hs,
  386.             tam_fdef->ff_fc[ c ].fc_vs,
  387.         foreground, background, (int) *s,
  388.             &(tam_fdef->ff_fc[ c ] ) );
  389.          x_pos += tam_fdef->ff_hs;
  390.          }
  391.       ++s;
  392.       }
  393.    }
  394.  
  395. /****************************************************************
  396.  
  397.    gr_strlen()
  398.  
  399. ****************************************************************/
  400.  
  401. unsigned int
  402. gr_strlen( string )
  403.    char *string;
  404.    {
  405.    char *s;
  406.    int size;
  407.  
  408.    s = string;
  409.    size = 0;
  410.    while ( *s != 0 )
  411.       {
  412.       size += tam_fdef->ff_hs;
  413.       ++s;
  414.       }
  415.    return size;
  416.    }
  417.  
  418. /****************************************************************
  419.  
  420.    gr_rectangle()
  421.  
  422. ****************************************************************/
  423.  
  424. gr_rectangle( screen, x1, y1, x2, y2, color, style )
  425.    int screen;
  426.    int x1, y1, x2, y2;
  427.    int color, style;
  428.    {
  429.    unsigned short *pattern;
  430.    int r;
  431.  
  432. #if    CHECK_PARAMS
  433.    if ( ( x1 < 0 ) || ( x1 > grwind->xmax ))
  434.       {
  435.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): x1 value is %d", x1 );
  436.       bw_error( bw_ebuf );
  437.       return BW_ERROR;
  438.       }
  439.    if ( ( x2 < 0 ) || ( x2 > grwind->xmax ))
  440.       {
  441.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): x2 value is %d", x2 );
  442.       bw_error( bw_ebuf );
  443.       return BW_ERROR;
  444.       }
  445.    if ( ( y1 < 0 ) || ( y1 > grwind->ymax ))
  446.       {
  447.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): y1 value is %d", y1 );
  448.       bw_error( bw_ebuf );
  449.       return BW_ERROR;
  450.       }
  451.    if ( ( y2 < 0 ) || ( y2 > grwind->ymax ))
  452.       {
  453.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): y2 value is %d", y2 );
  454.       bw_error( bw_ebuf );
  455.       return BW_ERROR;
  456.       }
  457. #endif
  458.  
  459.    tam_setscreen( screen );
  460.  
  461.    switch ( style )
  462.       {
  463.       case HOLLOW:
  464.          def_rectangle( screen, x1, y1, x2, y2, color, style );
  465.          return TRUE;
  466.          break;
  467.       case SOLID:
  468.          if ( color == WHITE )
  469.             {
  470.             pattern = patwhite;
  471.             }
  472.          else
  473.             {
  474.             pattern = patblack;
  475.             }
  476.          break;
  477.       case GRID:
  478.          pattern = patgray;
  479.          break;
  480.       case HATCH:
  481.          pattern = patltgray;
  482.          break;
  483.       }
  484.  
  485.    r = wrastop( w, tam_pixel, (unsigned short) 2,
  486.       tam_selected, tam_words( grwind->xmax + ADD_HIDDEN ),
  487.       (unsigned short) 0, (unsigned short) 0,
  488.       (unsigned short) x1,
  489.       (unsigned short) ( grwind->ymax - y2 ),
  490.       (unsigned short) abs(x2 - x1) + ADD_SIZE,
  491.       (unsigned short) abs(y2 - y1) + ADD_SIZE,
  492.       SRCPAT, DSTSRC, pattern );
  493.  
  494. #ifdef    DEBUG
  495.    if ( r == -1 )
  496.       {
  497.       wgoto( w, 0, 0 );
  498.       fprintf( stderr, "gr_rectangle(): wrastop() failed, error %d, screen %d.\n",
  499.          errno, screen );
  500.       kb_rx();
  501.       }
  502. #endif
  503.    }
  504.  
  505. /****************************************************************
  506.  
  507.    gr_circle()
  508.  
  509. ****************************************************************/
  510.  
  511. gr_circle( screen, x, y, radius, color, style )
  512.    int screen;
  513.    int x, y, radius;
  514.    int color, style;
  515.    {
  516.  
  517. #if    CHECK_PARAMS
  518.    if ( ( x < 0 ) || ( x > grwind->xmax ))
  519.       {
  520.       sprintf( bw_ebuf, "[pr:] gr_circle(): x value is %d", x );
  521.       bw_error( bw_ebuf );
  522.       return BW_ERROR;
  523.       }
  524.    if ( ( y < 0 ) || ( y > grwind->ymax ))
  525.       {
  526.       sprintf( bw_ebuf, "[pr:] gr_circle(): y value is %d", y );
  527.       bw_error( bw_ebuf );
  528.       return BW_ERROR;
  529.       }
  530. #endif
  531.  
  532.    def_circle( screen, x, y, radius, color, style );
  533.    }
  534.  
  535. /****************************************************************
  536.  
  537.    gr_ellipse()
  538.  
  539. ****************************************************************/
  540.  
  541. gr_ellipse( screen, x, y, x_radius, y_radius, mode, color, style )
  542.    int screen;
  543.    int x, y, x_radius, y_radius;
  544.    int mode, color, style;
  545.    {
  546.    }
  547.          
  548. /****************************************************************
  549.  
  550.    gr_clip()
  551.  
  552. ****************************************************************/
  553.  
  554. gr_clip( screen, mode, x1, y1, x2, y2 )
  555.    int screen;
  556.    int mode;
  557.    int x1, y1, x2, y2;
  558.    {
  559.    }
  560.  
  561. /****************************************************************
  562.  
  563.    gr_font()
  564.  
  565. ****************************************************************/
  566.  
  567. gr_font( screen, type, rq_height )
  568.    int screen;
  569.    int type, rq_height;
  570.    {
  571.  
  572.    if ( rq_height > 21 )
  573.       {
  574.       tam_fdef = tam_fhuge;
  575.       if ( tam_fdef > NULL )
  576.          {
  577.          grwind->fysize = tam_fdef->ff_vs;
  578.          grwind->fxsize = tam_fdef->ff_hs;
  579.          return TRUE;
  580.          }
  581.       }
  582.    else if ( rq_height > 14 )
  583.       {
  584.       tam_fdef = tam_flarge;
  585.       if ( tam_fdef > NULL )
  586.          {
  587.          grwind->fysize = tam_fdef->ff_vs;
  588.          grwind->fxsize = tam_fdef->ff_hs;
  589.          return TRUE;
  590.          }
  591.       }
  592.    else if ( rq_height > 9 )
  593.       {
  594.       tam_fdef = tam_fmed;
  595.       if ( tam_fdef > NULL )
  596.          {
  597.          grwind->fysize = tam_fdef->ff_vs;
  598.          grwind->fxsize = tam_fdef->ff_hs;
  599.          return TRUE;
  600.          }
  601.       }
  602.    else
  603.       {
  604.       tam_fdef = tam_fsmall;
  605.       if ( tam_fdef > NULL )
  606.          {
  607.          grwind->fysize = tam_fdef->ff_vs;
  608.          grwind->fxsize = tam_fdef->ff_hs;
  609.          return TRUE;
  610.          }
  611.       }
  612.  
  613.    tam_fdef = tam_fmed;        /* default if all failed */
  614.    grwind->fysize = tam_fdef->ff_vs;
  615.    grwind->fxsize = tam_fdef->ff_hs;
  616.    return TRUE;
  617.    }
  618.  
  619. /****************************************************************
  620.  
  621.    gr_blit()
  622.  
  623. ****************************************************************/
  624.  
  625. gr_blit( src, dst, x1, y1, x2, y2 )
  626.    int src, dst;
  627.    int x1, y1, x2, y2;
  628.    {
  629.    unsigned short *srcbase, *dstbase;
  630.    int r;
  631.    register int l;
  632.  
  633. #if    CHECK_PARAMS
  634.    if ( ( x1 < 0 ) || ( x1 > grwind->xmax ))
  635.       {
  636.       sprintf( bw_ebuf, "[pr:] gr_blit(): x1 value is %d", x1 );
  637.       bw_error( bw_ebuf );
  638.       return BW_ERROR;
  639.       }
  640.    if ( ( x2 < 0 ) || ( x2 > grwind->xmax ))
  641.       {
  642.       sprintf( bw_ebuf, "[pr:] gr_blit(): x2 value is %d", x2 );
  643.       bw_error( bw_ebuf );
  644.       return BW_ERROR;
  645.       }
  646.    if ( ( y1 < 0 ) || ( y1 > grwind->ymax ))
  647.       {
  648.       sprintf( bw_ebuf, "[pr:] gr_blit(): y1 value is %d", y1 );
  649.       bw_error( bw_ebuf );
  650.       return BW_ERROR;
  651.       }
  652.    if ( ( y2 < 0 ) || ( y2 > grwind->ymax ))
  653.       {
  654.       sprintf( bw_ebuf, "[pr:] gr_blit(): y2 value is %d", y2 );
  655.       bw_error( bw_ebuf );
  656.       return BW_ERROR;
  657.       }
  658.    if ( src == dst )
  659.       {
  660.       sprintf( bw_ebuf, "[pr:] gr_blit(): src == dst" );
  661.       bw_error( bw_ebuf );
  662.       return BW_ERROR;
  663.       }
  664. #endif
  665.  
  666.    if ( src == GR_PRIMARY )
  667.       {
  668.       srcbase = (unsigned short *) NULL;
  669.       dstbase = tam_hidden;
  670.       r = wrastop( w, srcbase,
  671.          tam_words( abs( x2 - x1 ) + ADD_SIZE ),
  672.          dstbase, tam_words( grwind->xmax + ADD_HIDDEN ),
  673.          (unsigned short) x1,
  674.          (unsigned short) y1,
  675.          (unsigned short) x1, 
  676.          (unsigned short) y1,
  677.          (unsigned short) abs(x2 - x1),
  678.          (unsigned short) abs(y2 - y1),
  679.          SRCSRC, DSTSRC, (unsigned short *) 0 );
  680.  
  681. #ifdef    DEBUG
  682.       if ( r == -1 )
  683.          {
  684.          wgoto( w, 0, 0 );
  685.          fprintf( stderr, "gr_blit(): wrastop() failed, error %d.\n", errno );
  686.          kb_rx();
  687.          }
  688. #endif
  689.       }
  690.    else
  691.       {
  692.       dstbase = (unsigned short *) NULL;
  693.       srcbase = tam_hidden;
  694.  
  695. #ifndef OLD_WAY
  696.       r = wrastop( w,
  697.          srcbase, tam_words( grwind->xmax + ADD_HIDDEN ),
  698.          dstbase, tam_words( grwind->xmax + ADD_HIDDEN ),
  699.          (unsigned short) x1,
  700.          (unsigned short) ( grwind->ymax - y2 ),
  701.          (unsigned short) x1,
  702.          (unsigned short) ( grwind->ymax - y2 ),
  703.          (unsigned short) abs(x2 - x1) + ADD_SIZE,
  704.          (unsigned short) abs(y2 - y1) + ADD_SIZE,
  705.          SRCSRC, DSTSRC, (unsigned short *) 0 );
  706. #else
  707.       for ( l = y1; l < y2; ++l )
  708.          {
  709.          r = wrastop( w,
  710.             srcbase, tam_words( grwind->xmax + ADD_HIDDEN ),
  711.             dstbase, tam_words( grwind->xmax + ADD_HIDDEN ),
  712.             (unsigned short) x1,
  713.             (unsigned short) ( grwind->ymax - l ),
  714.             (unsigned short) x1,
  715.             (unsigned short) ( grwind->ymax - l ),
  716.             (unsigned short) abs(x2 - x1) + ADD_SIZE,
  717.             (unsigned short) 1,
  718.             SRCSRC, DSTSRC, (unsigned short *) 0 );
  719. /*         kb_rx(); */
  720.          }
  721. #endif
  722.       
  723. #ifdef    DEBUG
  724.       if ( r == -1 )
  725.          {
  726.          wgoto( w, 0, 0 );
  727.          fprintf( stderr, "gr_blit(): wrastop() failed, error %d.\n", errno );
  728.          kb_rx();
  729.          }
  730. #endif
  731.       }
  732.    }
  733.  
  734. /****************************************************************
  735.  
  736.    tam_imsize()
  737.  
  738. ****************************************************************/
  739.  
  740. long
  741. tam_imsize( x1, y1, x2, y2 )
  742.    int x1, y1, x2, y2;
  743.    {
  744.    return ( sizeof( unsigned short)
  745.       * ( tam_words(abs(x2 - x1) + ADD_SIZE )
  746.       * ( abs(y2 - y1)) + ADD_SIZE ));
  747.    }
  748.  
  749. /****************************************************************
  750.  
  751.    gr_imsave()
  752.  
  753. ****************************************************************/
  754.  
  755. gr_imsave( screen, mode, x1, y1, x2, y2, image )
  756.    int screen; 
  757.    int mode, x1, y1, x2, y2;
  758.    int *image;
  759.    {
  760.    int r;
  761.    int carry_on;
  762.  
  763. #if    CHECK_PARAMS
  764.    if ( ( x1 < 0 ) || ( x1 > grwind->xmax ))
  765.       {
  766.       sprintf( bw_ebuf, "[pr:] gr_imsave(): x1 value is %d", x1 );
  767.       bw_error( bw_ebuf );
  768.       return BW_ERROR;
  769.       }
  770.    if ( ( x2 < 0 ) || ( x2 > grwind->xmax ))
  771.       {
  772.       sprintf( bw_ebuf, "[pr:] gr_imsave(): x2 value is %d", x2 );
  773.       bw_error( bw_ebuf );
  774.       return BW_ERROR;
  775.       }
  776.    if ( ( y1 < 0 ) || ( y1 > grwind->ymax ))
  777.       {
  778.       sprintf( bw_ebuf, "[pr:] gr_imsave(): y1 value is %d", y1 );
  779.       bw_error( bw_ebuf );
  780.       return BW_ERROR;
  781.       }
  782.    if ( ( y2 < 0 ) || ( y2 > grwind->ymax ))
  783.       {
  784.       sprintf( bw_ebuf, "[pr:] gr_imsave(): y2 value is %d", y2 );
  785.       bw_error( bw_ebuf );
  786.       return BW_ERROR;
  787.       }
  788. #endif
  789.  
  790.    tam_setscreen( screen );
  791.  
  792.    if ( mode == TRUE )
  793.       {
  794.  
  795.  
  796.       /* find an available buffer */
  797.  
  798.       carry_on = TRUE;
  799.       r = 0;
  800.       while( ( carry_on == TRUE ) && ( r < IMAGES ) )
  801.      {
  802.      if ( tam_images[ r ] == NULL )
  803.         {
  804.         carry_on = FALSE;
  805.         }
  806.      else
  807.         {
  808.         ++r;
  809.         }
  810.      }
  811.  
  812.       if ( r >= IMAGES )
  813.      {
  814.      bw_error( "No more slots for image storage" );
  815.      return FALSE;
  816.      }
  817.  
  818.       *image = r;
  819.  
  820.       /* get memory */
  821.  
  822.       if ( ( tam_images[ *image ] = malloc( (size_t) tam_imsize(
  823.      x1, grwind->ymax - y2,
  824.      x2, grwind->ymax - y1 ) ) ) == NULL )
  825.      {
  826.      bw_error( "Out of memory to store image" );
  827.      return FALSE;
  828.      }
  829.  
  830.       /* perform the save operation */
  831.  
  832.       r = wrastop( w, tam_selected,
  833.          tam_words( grwind->xmax + ADD_HIDDEN ),
  834.      (unsigned short *) tam_images[ *image ],
  835.          tam_words( abs(x2 - x1)),
  836.          (unsigned short) x1, 
  837.          (unsigned short) ( grwind->ymax - y2 ),
  838.          (unsigned short) 0, (unsigned short) 0,
  839.          (unsigned short) abs(x2 - x1) + ADD_SIZE,
  840.          (unsigned short) abs(y2 - y1) + ADD_SIZE,
  841.          SRCSRC, DSTSRC, (unsigned short *) 0 );
  842.  
  843. #ifdef    DEBUG
  844.       if ( r == -1 )
  845.          {
  846.          wgoto( w, 0, 0 );
  847.          fprintf( stderr, "gr_imsave(): wrastop() failed, error %d.\n", errno );
  848.          kb_rx();
  849.          }
  850. #endif
  851.       }
  852.    else
  853.       {
  854.       r = wrastop( w, (unsigned short *) tam_images[ *image ],
  855.          tam_words( abs(x2 - x1)),
  856.          tam_selected, tam_words( grwind->xmax + ADD_HIDDEN ),
  857.          (unsigned short) 0, (unsigned short) 0,
  858.          (unsigned short) x1,
  859.          (unsigned short) ( grwind->ymax - y2 ),
  860.          (unsigned short) abs(x2 - x1) + ADD_SIZE,
  861.          (unsigned short) abs(y2 - y1) + ADD_SIZE,
  862.          SRCSRC, DSTSRC, (unsigned short *) 0 );
  863.  
  864. #ifdef    DEBUG
  865.       if ( r == -1 )
  866.          {
  867.          wgoto( w, 0, 0 );
  868.          fprintf( stderr, "gr_imsave(): wrastop() failed, error %d.\n", errno );
  869.          kb_rx();
  870.          }
  871. #endif
  872.       }
  873.    }
  874.  
  875. /****************************************************************
  876.  
  877.     gr_imfree()
  878.  
  879. ****************************************************************/
  880.  
  881. gr_imfree( image )
  882.    int image;
  883.    {
  884.  
  885. #ifdef DEBUG
  886.    if ( tam_images[ image ] == NULL )
  887.       {
  888.       bw_error( "gr_imfree(): NULL image requested" );
  889.       return FALSE;
  890.       }
  891. #endif
  892.  
  893.    free( tam_images[ image ] );
  894.    tam_images[ image ] = NULL;
  895.  
  896.    }
  897.  
  898. /****************************************************************
  899.  
  900.    gr_mouse()
  901.  
  902. ****************************************************************/
  903.  
  904. gr_mouse( mode, x, y, buttons )
  905.    int mode;
  906.    int *x, *y;
  907.    int *buttons;
  908.    {
  909.    static int b, r;
  910.    
  911.    if (( mode == STATUS ) || ( mode == SAMPLE ))
  912.       {
  913.  
  914. #if WRITE_OUTPUT
  915.    fprintf( outfile, "gr_mouse() STATUS: enter\n" );
  916.    fflush( outfile );
  917. #endif
  918.  
  919.       if ( tam_mready == FALSE )
  920.          {
  921.          att_rxstat();
  922.          }
  923.       return tam_mready;
  924.       }
  925.    else if ( mode == WAIT )
  926.       {
  927.  
  928. #if WRITE_OUTPUT
  929.    fprintf( outfile, "gr_mouse() WAIT: enter\n" );
  930.    fflush( outfile );
  931. #endif
  932.  
  933.       while( tam_mready == FALSE )
  934.          {
  935.          att_rxstat();
  936.          }
  937.       *x = tam_mx;
  938.       *y = grwind->ymax - tam_my;
  939.       tam_mready = FALSE;
  940.       return TRUE;
  941.       }
  942.    }
  943.  
  944. tam_setscreen( screen )
  945.    int screen;
  946.    {
  947.    if ( screen == GR_HIDDEN )
  948.       {
  949.       tam_selected = tam_hidden;
  950.       }
  951.    else
  952.       {
  953.       tam_selected = (unsigned short *) 0;
  954.       }    
  955.    }
  956.  
  957. tam_readfont( fontfile, sptr )
  958.    char *fontfile;
  959.    struct fntdef **sptr;
  960.    {
  961.    FILE *ff_fp;
  962.    int size;
  963.    char *m;
  964.  
  965.    /* open the font file */
  966.  
  967.    if ( ( ff_fp = fopen( fontfile, "r" )) == NULL )
  968.       {
  969.       fprintf( stderr, "Failed to open font file\n" );
  970.       wgetc( w );
  971.       wexit( 1 );
  972.       }
  973.  
  974.    /* find the size of the file */
  975.  
  976.    size = 0;
  977.    while( !feof( ff_fp ) )
  978.       {
  979.       ++size;
  980.       fgetc( ff_fp );
  981.       }
  982.  
  983.    /* allocate memory for the file */
  984.  
  985.    if ( ( m = malloc( size )) == NULL )
  986.       {
  987.       fprintf( stderr, "No memory for font file <%s> \n",
  988.          fontfile );
  989.       *sptr = NULL;
  990.       wgetc( w );
  991.       return BW_ERROR;
  992.       }
  993.    *sptr = (struct fntdef *) m;
  994.  
  995. #ifdef    OLD_DEBUG
  996.    clear();
  997.    wgoto( w, 0, 0 );
  998.    fprintf( stderr, "Loading font <%s> \n", fontfile );
  999.    fprintf( stderr, "Size %d bytes sptr 0x%08lX top 0x%08lX \n",
  1000.       size, (long) *sptr, (long) *sptr + size );
  1001.    wgetc( w );
  1002.    clear();
  1003.    wgoto( 0, 0 );
  1004. #endif
  1005.  
  1006.    /* now read the file into memory */
  1007.  
  1008.    rewind( ff_fp );
  1009.    while( !feof( ff_fp ) )
  1010.       {
  1011.       *m = fgetc( ff_fp );
  1012.       ++m;
  1013.       }
  1014.  
  1015.    /* close the font file */
  1016.  
  1017.    fclose( ff_fp );
  1018.  
  1019. #ifdef    OLD_DEBUG
  1020.    fprintf( stderr, "Loaded font <%s>, %d by %d pixels\n",
  1021.       fontfile, (*sptr)->ff_vs, (*sptr)->ff_hs );
  1022.    wgetc( w );
  1023. #endif
  1024.  
  1025.    }
  1026.  
  1027. tam_char( screen, x, y, icon, x_size, y_size,
  1028.    foreground, background, c, cdef )
  1029.    int screen, x, y, x_size, y_size, foreground, background, c;
  1030.    unsigned short *icon;
  1031.    struct fcdef *cdef;
  1032.    {
  1033.    int r;
  1034.  
  1035.    /* paint the background */
  1036.  
  1037.    gr_rectangle( screen, x, y,
  1038.       x + tam_fdef->ff_hs, y + tam_fdef->ff_vs,
  1039.       background, SOLID );
  1040.  
  1041.    /* call wrastop() to display the character as an icon */
  1042.  
  1043.    r = wrastop( w, icon, tam_words( y_size ),
  1044.       tam_selected, tam_words( grwind->xmax + ADD_HIDDEN ),
  1045.       (unsigned short) 0, (unsigned short) 0,
  1046.       (unsigned short) x + cdef->fc_ha,
  1047.       (unsigned short) (grwind->ymax - y) + ( cdef->fc_va - 1 ),
  1048.       (unsigned short) x_size,
  1049.       (unsigned short) y_size,
  1050.       SRCSRC, DSTXOR, (unsigned short *) 0 );
  1051.  
  1052. #ifdef    OLD_DEBUG
  1053.    wgoto( w, 10, 0 );
  1054.    if ( r == -1 )
  1055.       {
  1056.       fprintf( stderr, "tam_char(): wrastop() failed, error %d, screen %d.\n",
  1057.          errno, screen );
  1058.       wgetc( w );
  1059.       }
  1060. #ifdef OLD_STUFF
  1061.    fprintf( stderr, "Character 0x%X <%c>:  offset %d (0x%04X\n",
  1062.       c, c, cdef->fc_mr, cdef->fc_mr );
  1063.    fprintf( stderr, "  x_size %d, y_size %d, words %d \n",
  1064.       x_size, y_size, tam_words( y_size ) );
  1065.    fprintf( stderr, "  fc_ha %d, fc_va %d, fc_hi %d, fc_vi %d \n",
  1066.       cdef->fc_ha, cdef->fc_va, cdef->fc_hi, cdef->fc_vi );
  1067.    fprintf( stderr, "  x pos %d, y pos %d\n",
  1068.       x, y );
  1069.    fprintf( stderr, "  icon %08lX selected %08lX\n",
  1070.       (long) icon, (long) tam_selected );
  1071.    wgetc( w );
  1072.    clear();
  1073. #endif
  1074. #endif
  1075.    }
  1076.  
  1077. /* determine the number of 16-bit words necessary
  1078.    for horizontal axis */
  1079.  
  1080. unsigned short
  1081. tam_words( pixels )
  1082.    int pixels;
  1083.    {
  1084.    return ((( pixels + 15) / 16 ) * 2);
  1085.    }
  1086.  
  1087. #ifdef T_WRASTOP
  1088. t_wrastop( tw, srcbase, srcwidth, dstbase, dstwidth,
  1089.    srcx, srcy, dstx, dsty, width, height,
  1090.    srcop, dstop, pattern )
  1091.    int tw;
  1092.    unsigned short *srcbase, *dstbase, *pattern;
  1093.    unsigned short srcwidth, dstwidth;
  1094.    unsigned short srcx, srcy, dstx, dsty;
  1095.    char srcop, dstop;
  1096.    {
  1097.  
  1098.  
  1099.    if ( tam_wtest == TRUE )
  1100.       {
  1101. /*      gr_rectangle( GR_PRIMARY, 0, grwind->ymax - 40,
  1102.          grwind->xmax, grwind->ymax, BLACK, SOLID ); */
  1103.       wgoto( w, 0, 0 );
  1104.       fprintf( stderr, "test wrastop(): srcwidth %d, dstwidth %d\n",
  1105.          srcwidth, dstwidth );
  1106.       kb_rx();
  1107. /*      gr_rectangle( GR_PRIMARY, 0, grwind->ymax - 40,
  1108.          grwind->xmax, grwind->ymax, BLACK, SOLID ); */
  1109.       wgoto( w, 0, 0 );
  1110.       fprintf( stderr, "test wrastop(): srcbase %08lx, dstbase %08lx\n",
  1111.          (long) srcbase, (long) dstbase );
  1112.       kb_rx();
  1113. /*      gr_rectangle( GR_PRIMARY, 0, grwind->ymax - 40,
  1114.          grwind->xmax, grwind->ymax, BLACK, SOLID ); */
  1115.       wgoto( w, 0, 0 );
  1116.       fprintf( stderr, "test wrastop(): srcx %d, srcy %d, dstx %d dsty %d\n",
  1117.          srcx, srcy, dstx, dsty );
  1118.       kb_rx();
  1119. /*      gr_rectangle( GR_PRIMARY, 0, grwind->ymax - 40,
  1120.          grwind->xmax, grwind->ymax, BLACK, SOLID ); */
  1121.       wgoto( w, 0, 0 );
  1122.       fprintf( stderr, "test wrastop(): width %d, height %d\n",
  1123.          width, height );
  1124.       kb_rx();
  1125.       }
  1126.  
  1127.    return wrastop( tw, srcbase, srcwidth, dstbase, dstwidth,
  1128.       srcx, srcy, dstx, dsty, width, height,
  1129.       srcop, dstop, pattern );
  1130.  
  1131.    }
  1132. #endif
  1133.